package com.clz.controller;

import com.clz.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

//    @RequestMapping("login")
//    public ModelAndView login(String userName,String password){
//        ModelAndView mv =new ModelAndView();
//        if ("admin".equals(userName)&&"123".equals(password)){
//            mv.addObject("userName", userName);
//            mv.setViewName("main");
//        }else {
//            mv.setViewName("login");
//        }
//        return mv;
//    }

    @RequestMapping("login")
//    @PostMapping("login")
//    @GetMapping("login")
    public String login(User user, HttpSession session){
        if ("admin".equals(user.getUserName())&&"123".equals(user.getPassword())){
            session.setAttribute("user",user);
            return "redirect:hi.clz" ;
        }else {
            return "login";
        }
    }

//    @RequestMapping("login")
////    @PostMapping("login")
////    @GetMapping("login")
//    public String login(User user,Model model){
//        if ("admin".equals(user.getUserName())&&"123".equals(user.getPassword())){
//          model.addAttribute("user",user);
//            return "redirect:main.jsp" ;
//        }else {
//            return "login";
//        }
//    }
//
//    @GetMapping("/index")
    //得到一个ID号的请求，然后把请求附带的ID绑定给参数id
//    public ModelAndView toIndex(@RequestParam("id") int id){
//        System.out.println("id===="+id);
//        return new ModelAndView();
//    }


    @RequestMapping("/index/{id}")
    //这种写法，在请求的时候更安全，然后把id赋给参数id
    public ModelAndView toIndex(@PathVariable("id") int id){
        System.out.println("id===="+id);
        Map map = new HashMap<>();
        map.put("id", id);
         return new ModelAndView("index",map);
    }


    @RequestMapping("/showJson")
    public ModelAndView showJsonInfo(){
       ModelAndView mv =new ModelAndView();
       User user =new User();
       user.setUserName("曹操");
       user.setPassword("123456");
       mv.addObject("user" , user);
       mv.setView(new MappingJackson2JsonView() );
       return  mv;
    }
    @RequestMapping("/showList")
    @ResponseBody
    public List<User> showList(){
        List<User> list =new ArrayList<>();
        User user =new User();
        user.setUserName("曹操");
        user.setPassword("123456");

        User user2 =new User();
        user2.setUserName("曹植");
        user2.setPassword("123456");

        User user3 =new User();
        user3.setUserName("曹匹");
        user3.setPassword("123456");
        list.add(user);
        list.add(user2);
        list.add(user3);
        return list;

    }
}
